package com.example.orderfood.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.Toolbar;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import com.example.orderfood.R;
import com.example.orderfood.adapter.OrderBusinessAdapter;
import com.example.orderfood.adapter.OrderBusinessFinishAdapter;
import com.example.orderfood.bean.OrderBean;
import com.example.orderfood.dao.OrderDao;

import java.util.List;

/**
    商家的订单完成界面
 */
public class OrderFinishActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_finish);
        //通过按钮的点击时间实现修改密码update_business_pwd_button
        SharedPreferences sharedPreferences=getSharedPreferences("data", Context.MODE_PRIVATE);
        String account=sharedPreferences.getString("account","");//这里面的account是代表这个商家的用户
        ListView listView=findViewById(R.id.business_list_view_order);//加载动态列表
        List<OrderBean> list = OrderDao.getAllOrderByBusinessFinish(account);
        if(list!=null&&list.size()>0){
            OrderBusinessFinishAdapter adapter = new OrderBusinessFinishAdapter(this, list);
            listView.setAdapter(adapter);
        }else{
            listView.setAdapter(null);
        }
        //接下来实现查询
        //import androidx.appcompat.widget.SearchView;
        SearchView search=findViewById(R.id.business_search_search);
        search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                if(s.equals("")){
                    List<OrderBean> list = OrderDao.getAllOrderByBusinessFinish(account);
                    if(list!=null&&list.size()>0){
                        OrderBusinessFinishAdapter adapter = new OrderBusinessFinishAdapter(OrderFinishActivity.this, list);
                        listView.setAdapter(adapter);
                    }else{
                        listView.setAdapter(null);
                    }
                }else{

                    List<OrderBean> list = OrderDao.getAllOrderByBusinessFinish(account,s);
                    if(list!=null&&list.size()>0){
                        OrderBusinessFinishAdapter adapter = new OrderBusinessFinishAdapter(OrderFinishActivity.this, list);
                        listView.setAdapter(adapter);
                    }else{
                        listView.setAdapter(null);
                    }

                }
                return true;
            }
            @Override
            public boolean onQueryTextChange(String s) {
                if(s.equals("")){
                    List<OrderBean> list = OrderDao.getAllOrderByBusinessFinish(account);
                    if(list!=null&&list.size()>0){
                        OrderBusinessFinishAdapter adapter = new OrderBusinessFinishAdapter(OrderFinishActivity.this, list);
                        listView.setAdapter(adapter);
                    }else{
                        listView.setAdapter(null);
                    }
                }else{

                    List<OrderBean> list = OrderDao.getAllOrderByBusinessFinish(account,s);
                    if(list!=null&&list.size()>0){
                        OrderBusinessFinishAdapter adapter = new OrderBusinessFinishAdapter(OrderFinishActivity.this, list);
                        listView.setAdapter(adapter);
                    }else{
                        listView.setAdapter(null);
                    }

                }
                return true;
            }
        });
        Toolbar toolbar =findViewById(R.id.business_toolbar);//加载动态列表
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
    }
}